package com.wyzz.hr.io.extend;

import java.text.MessageFormat;
import java.util.*;

import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Inmatch;
import com.wyzz.hr.pojo.InmatchModel;
import com.wyzz.hr.util.MessageForField;
import com.wyzz.hr.util.reflect.ObjectProperty;
import com.wyzz.hr.util.reflect.SetObjectProperty;

public class CommonParameters {

	public CommonParameters(InmatchModel inmatchModel, Map matchMap,
			IOMessages ioMessages, Employee currEmp, String authorityCondition,
			String importParameter[]) {
		setMatchMap(matchMap);
		setInmatchModel(inmatchModel);
		setIoMessages(ioMessages);
		setCurrEmp(currEmp);
		setAuthCondition(authorityCondition);
		if (importParameter.length > 0)
			setImportParameter(importParameter[0]);
	}

	public Integer getColNum(String fieldName) {
		if (matchMap.containsKey(fieldName))
			return ((Inmatch) matchMap.get(fieldName)).getImSortId();
		else
			return null;
	}

	public String getDesc(String fieldName) {
		if (matchMap.containsKey(fieldName))
			return ((Inmatch) matchMap.get(fieldName)).getImFieldDesc();
		else
			return fieldName;
	}

	public void addMessage(String contentModle, Integer msgType,
			Integer rowNum, String fieldName[]) {
		Integer colNum;
		String paras[];
		if (fieldName.length == 0) {
			colNum = null;
			paras = new String[0];
		} else {
			colNum = getColNum(fieldName[0]);
			paras = new String[fieldName.length];
			for (int i = 0; i < fieldName.length; i++)
				paras[i] = getDesc(fieldName[i]);

		}
		ioMessages.addMessage(new IOMessageSingle(MessageFormat.format(
				contentModle, (Object[]) paras), msgType, new Integer[] {
				rowNum, colNum }));
	}

	public void addErrorMessage(String contentModle, Integer rowNum,
			String fieldName[]) {
		addMessage(contentModle, Integer.valueOf(-1), rowNum, fieldName);
	}

	public void addMessages(List msgList, Integer rowNum) {
		MessageForField mff;
		for (Iterator i$ = msgList.iterator(); i$.hasNext(); addMessage(mff
				.getMessageModel(), mff.getMsgType(), rowNum, mff.getFields()))
			mff = (MessageForField) i$.next();

	}

	public void copy1To2(Object obj1, Object obj2) throws Exception {
		for (int i = 0; i < inmatchModel.getImList().size(); i++) {
			String fieldName = ((Inmatch) inmatchModel.getImList().get(i))
					.getImImb().getImbFieldName();
			if (fieldName.indexOf(".") != -1)
				fieldName = fieldName.substring(0, fieldName.indexOf("."));
			SetObjectProperty.setObjectProperty(obj2, fieldName, ObjectProperty
					.getObjectFinalProperty(obj1, fieldName));
		}

	}

	public Map getMatchMap() {
		return matchMap;
	}

	public void setMatchMap(Map matchMap) {
		this.matchMap = matchMap;
	}

	public InmatchModel getInmatchModel() {
		return inmatchModel;
	}

	public void setInmatchModel(InmatchModel inmatchModel) {
		this.inmatchModel = inmatchModel;
	}

	public IOMessages getIoMessages() {
		return ioMessages;
	}

	public void setIoMessages(IOMessages ioMessages) {
		this.ioMessages = ioMessages;
	}

	public Employee getCurrEmp() {
		return currEmp;
	}

	public void setCurrEmp(Employee currEmp) {
		this.currEmp = currEmp;
	}

	public String getImportParameter() {
		return importParameter;
	}

	public void setImportParameter(String importParameter) {
		this.importParameter = importParameter;
	}

	public String getAuthCondition() {
		return authCondition;
	}

	public void setAuthCondition(String authorityCondition) {
		authCondition = authorityCondition;
	}

	private InmatchModel inmatchModel;
	private Map matchMap;
	private IOMessages ioMessages;
	private Employee currEmp;
	private String authCondition;
	private String importParameter;
}
